<?php

/*
 * Controller/TemplateController.php
 */

namespace RHX\Controller;

use Zend\View\Model\ViewModel;
use RHX\Helper\RHDateTime;
use RHX\Model\Object\XRules;
use Zend\View\Model\JsonModel;
use RHX\Model\Object\EmployeTemplate;

class TemplateController extends RHXController {

    /**
     * page d'index
     *
     * @return array
     */
    public function indexAction() {
	// Tables
	$siteTable		 = $this->getService('Table\SiteTable');
	$codeActiviteTable	 = $this->getService('Table\CodeActiviteTable');

	// Liste des Codes Activités
	$codeList	 = $codeActiviteTable->getAll();
	// Parcours des codes
	$codes		 = array();
	$codec		 = array();
	$colors		 = array();
	foreach ($codeList as $c) {
	    $codes[]		 = $c;
	    $codec[$c->id]		 = $c->code;
	    $codec[$c->code]	 = $c->id;
	    $colors[$c->code]	 = $c->couleur;
	}

	// ID Site
	$siteId	 = $this->params()
		->fromRoute('id', -1);
	// Afficher
	$refresh = $this->params()
		->fromRoute('other1', 0);
	// Page
	$page	 = $this->params()
		->fromRoute('other2', 1);
	// Par Page
	$perPage = $this->params()
		->fromRoute('other3', 30);

	// Site en cours
	$site = null;
	if ($siteId < 0) {
	    $site	 = $this->navigation->offsetGet('site');
	    $refresh = 1;
	}
	else {
	    $site = $siteTable->get($siteId);
	    $this->changeSite($siteId);
	}

	// Liste des Sites
	$siteListe	 = $siteTable->getAll();
	$sites		 = array();

	// Parcours de la liste des Sites
	foreach ($siteListe as $s) {
	    $sites[] = $s;
	}

	return array(
	    'site'		 => $site,
	    'sites'		 => $sites,
	    'page'		 => $page,
	    'perPage'	 => $perPage,
	    'refresh'	 => ((int) $refresh == 1) ? true : false,
	    'codes'		 => $codes,
	    'codec'		 => $codec,
	    'colors'	 => $colors,
	);
    }

    /**
     * Contenu de la page d'index
     * 
     * @return array
     */
    public function loadIndexAction() {
	// Changement de layout
	$this->layout('layout/ajax');

	// Données en Post
	$post	 = $this->getRequest()
		->getPost();
	$siteId	 = $post->get('site');

	// Tables
	$planningTemplateTable	 = $this->getService('Table\PlanningTemplateTable');
	$siteTable		 = $this->getService('Table\SiteTable');

	// Liste des Plannings Type pour le site données
	$templates = $planningTemplateTable->getAllBySite($siteId);

	// Site en Cours
	$site = $siteTable->get($siteId);
	$this->changeSite($siteId);

	return array(
	    'templates'	 => $templates,
	    'site'		 => $site,
	);
    }

    /**
     * Liste des templates
     *
     * @return array
     */
    public function templateListAction() {
	// Changement de layout
	$this->layout('layout/ajax');

	// Table
	$templateListTable = $this->getService('Table\PlanningTemplateTable');

	// Post
	$post = $this->getRequest()
		->getPost();

	$siteId = $post->get('site');

	// Planning Type pour le site
	$templateList = $templateListTable->getAllBySite($siteId);

	return array(
	    'templateList' => $templateList,
	);
    }

    /**
     * Edition des templates
     *
     * @return array
     */
    public function editAction() {
	// Changement de layout
	$this->layout('layout/ajax');

	// Table
	$employeTemplateTable	 = $this->getService('Table\EmployeTemplateTable');
	$planningTemplateTable	 = $this->getService('Table\PlanningTemplateTable');
	$codeTable		 = $this->getService('Table\CodeActiviteTable');

	// Route
	$templateId = $this->params()
		->fromRoute('id', 0);

	// Templates
	//$templates		 = $employeTemplateTable->getTemplates($templateId);
        $templates		 = $employeTemplateTable->getTemplatesEx2($templateId);
	$planningTemplate	 = $planningTemplateTable->getTemplate($templateId);

	// Codes
	$codeList	 = $codeTable->getAll();
	$codec		 = array();
	$codes		 = array();
	foreach ($codeList as $c) {
	    $codec[$c->id]	 = $c->code;
	    $codes[]	 = $c;
	}

	// Post
	$post		 = $this->getRequest()
		->getPost();
	$nohead		 = $post->get('nohead') == '1' ? true : false;
	$fullscreen	 = $post->get('fullscreen') == '1' ? true : false;
	$version	 = (int) $post->version;
	if (!$version) {
	    $version = $planningTemplate->getVersion();
	}

	// Filtrage du template
	/*$templateList = array();
	foreach ($templates as $tpl) {
	    $versionArray = str_split($tpl->version);
	    if (isset($versionArray[$version - 1])) {
		if ((int) $versionArray[$version - 1] == 1) {
		    $templateList[] = $tpl;
		}
	    }
	}*/

	return array(
	    'templates'		 => $templates,
	    'planningTemplate'	 => $planningTemplate,
	    'codec'			 => $codec,
	    'codes'			 => $codes,
	    'nohead'		 => $nohead,
	    'fullscreen'		 => $fullscreen,
	    'template'		 => $templateId,
	    'versionMax'		 => $planningTemplate->getVersion(),
	    'version'		 => $version,
	);
    }

    /**
     * Renommer template
     *
     * @return array
     */
    public function renameAction() {
	// Changement de layout
	$this->layout('layout/ajax');

	// Post
	$post = $this->getRequest()
		->getPost();

	// Tables
	$planningTemplateTable = $this->getService('Table\PlanningTemplateTable');

	// Save
	$planningTemplate = new \RHX\Model\Object\PlanningTemplate($planningTemplateTable->getAdapter(), $post);
	$planningTemplateTable->save($planningTemplate);

	return array(
	    'planningTemplate' => $planningTemplate,
	);
    }

    /**
     * Affichage de l'interface de saisie de nouveau planning type
     * 
     * @return \Zend\View\Model\ViewModel
     */
    public function addNewAction() {
	// changement de layout
	$this->layout('layout/ajax');
	// post
	$post		 = $this->getRequest()
		->getPost();
	$choice		 = (int) $post->choice;
	$site		 = (int) $post->site;
	$copy		 = (int) $post->copy;
	// vue
	$view		 = new ViewModel();
	$view->setTemplate('rhx/template/new.phtml');
	// manager
	$templateManager = new \RHX\Model\Manager\TemplateManager($this->getAdapter());
	$data		 = null;
	switch ($choice) {
	    case 3:
		$data	 = $templateManager->loadNewCopy($site, $copy);
		break;
	    case 2:
		$data	 = $templateManager->loadNewEmpty($site, true);
		break;
	    case 1:
	    default:
		$data	 = $templateManager->loadNewEmpty($site);
		break;
	}
	$view->setVariables(array(
	    'templates' => $data['templates'],
	    'colors' => $templateManager->getColors(),
	    'site' => $templateManager->getSite($site),
	    'codec' => $templateManager->getCodec(),
	    'codes' => $templateManager->getCodes(),
	));
	
	return $view;
    }

    /**
     * Ajouter nouveau
     *
     * @return \Zend\View\Model\ViewModel
     */
    public function addNewAction___() {
	// Changement de layout
	$this->layout('layout/ajax');

	// Tables
	$employeTemplateTable	 = $this->getService('Table\EmployeTemplateTable');
	$planningTemplateTable	 = $this->getService('Table\PlanningTemplateTable');
	$employeTable		 = $this->getService('Table\EmployeTable');
	$siteTable		 = $this->getService('Table\SiteTable');

	// Post
	$post = $this->getRequest()
		->getPost();

	// Choix
	$choice	 = $post->get('choice');
	$site	 = $post->get('site');
	$copy	 = $post->get('copy');

	// Changement de Layout et Assignation de variable
	$view = null;
	switch ((int) $choice) {
	    case 2: // Disponibilité
		$variables	 = $this->newEmpty($site);
		$codeListe	 = $this->getService('Table\CodeActiviteTable')->getAll();
		// Parcours de la liste des codes
		$codec		 = array();
		foreach ($codeListe as $c) {
		    $codec[$c->id]	 = $c->code;
		    $codec[$c->code] = $c->id;
		}
		$variables['codec']	 = $codec;
		$view			 = new ViewModel($variables);
		$view->setTemplate('rhx/template/new-disponibilite.phtml');
		break;
	    case 3: // Copie
		$variables		 = $this->newCopy($site, $copy);
		$codeListe		 = $this->getService('Table\CodeActiviteTable')->getAll();
		// Parcours de la liste des codes
		$codec			 = array();
		foreach ($codeListe as $c) {
		    $codec[$c->id]	 = $c->code;
		    $codec[$c->code] = $c->id;
		}
		$variables['codec']	 = $codec;
		$view			 = new ViewModel($variables);
		$view->setTemplate('rhx/template/new-copy.phtml');
		break;
	    case 1: // Vide
	    default:
		$variables		 = $this->newEmpty($site);
		$view			 = new ViewModel($variables);
		$view->setTemplate('rhx/template/new-empty.phtml');
		break;
	}

	return $view;
    }

    /**
     * Nouveau planning type
     *
     * @param int $site
     * @return array
     */
    public function newEmpty($site) {
	// Tables
	$employeTable		 = $this->getService('Table\EmployeTable');
	$siteTable		 = $this->getService('Table\SiteTable');
	$codeActiviteTable	 = $this->getService('Table\CodeActiviteTable');

	// Liste des employés
	$employeList	 = $employeTable->getEmployesBySite($site);
	$employes	 = array();
	foreach ($employeList as $e) {
	    $employes[$e->matricule] = $e;
	}

	// Site
	$site = $siteTable->get($site);

	// Codes
	$codeList	 = $codeActiviteTable->getAll();
	$colors		 = array();
	foreach ($codeList as $c) {
	    $colors[$c->code] = $c->couleur;
	}

	return array(
	    'employes'	 => $employes,
	    'site'		 => $site,
	    'colors'	 => $colors,
	);
    }

    /**
     * Popin pour les nouveaux planning type
     * 
     * @return array
     */
    public function newPopinEmptyAction() {
	// Changement de layout
	$this->layout('layout/ajax');

	// Tables
	$codeTable = $this->getService('Table\CodeActiviteTable');

	// Liste des codes
	$codeList	 = $codeTable->getAll();
	$codes		 = array();
	// Parcours
	foreach ($codeList as $code) {
	    $codes[] = $code;
	}

	// Post
	$post = $this->getRequest()
		->getPost();

	return array(
	    'codes'		 => $codes,
	    'matricule'	 => $post->get('matricule'),
	    'prefix'	 => $post->get('prefix'),
	);
    }
    
    /**
     *  Sauvegarde du nouveau template
     * 
     * @return ViewModel
     */
    public function saveNewAction(){
	$this->layout('layout/ajax');
	$post = $this->getRequest()
		->getPost();
	$templateManager = new \RHX\Model\Manager\TemplateManager($this->getAdapter());
	$data = $templateManager->saveNew($post);
	
	return array(
	    'post' => $post,
	    'templates' => $data['templates'],
	    'planningTemplate' => $data['planningTemplate'],
	);
    }
    
    /**
     * Affichage du popover
     * 
     * @return array
     */
    public function popoverAction(){
	$this->layout('layout/ajax');
	$manager = new \RHX\Model\Manager\TemplateManager($this->getAdapter());
	$post = $this->getRequest()
		->getPost();
	
	return array(
	    'codes' => $manager->getCodes(),
	    'data' => $manager->getPopoverData($post),
	    'prefix' => $post->prefix,
	    'matricule' => $post->matricule,
	    'id' => (int)$post->id,
	);
    }

    public function saveEmptyAction() {
	// Changement de layout
	$this->layout('layout/ajax');

	// Table
	$employeTemplateTable	 = $this->getService('Table\EmployeTemplateTable');
	$planningTemplateTable	 = $this->getService('Table\PlanningTemplateTable');

	// Post
	$post = $this->getRequest()
		->getPost();

	// Création du planning dans la liste des plannings
	$planningTemplate		 = new \RHX\Model\Object\PlanningTemplate($planningTemplateTable->getAdapter());
	$planningTemplate->site_id	 = $post->get('site');
	$planningTemplate->libelle	 = $post->get('libelle');
	$planningTemplate->creation	 = date('Y-m-d');
	$planningTemplate->edition	 = date('Y-m-d');
	$planningTemplate->template	 = 0;
	// Sauvegarde
	$planningTemplateId		 = $planningTemplateTable->save($planningTemplate);
	$planningTemplate->template	 = $planningTemplateId;
	$planningTemplate->id		 = $planningTemplateId;
	$planningTemplateTable->save($planningTemplate);

	// Liste des employés
	$employes = $post->get('employes');
	// parcours de la liste des employés
	foreach ($employes as $matricule) {
	    $template			 = new \RHX\Model\Object\EmployeTemplate($employeTemplateTable->getAdapter());
	    $template->matricule		 = $matricule;
	    $template->template		 = $planningTemplate->template;
	    // lundi
	    $template->lundi_code		 = $post->get("$matricule-lundi-code");
	    $template->lundi_entree		 = $post->get("$matricule-lundi-entree");
	    $template->lundi_sortie		 = $post->get("$matricule-lundi-sortie");
	    $template->lundi_pause		 = $post->get("$matricule-lundi-pause");
	    $template->lundi_repas		 = $post->get("$matricule-lundi-repas");
	    // mardi
	    $template->mardi_code		 = $post->get("$matricule-mardi-code");
	    $template->mardi_entree		 = $post->get("$matricule-mardi-entree");
	    $template->mardi_sortie		 = $post->get("$matricule-mardi-sortie");
	    $template->mardi_pause		 = $post->get("$matricule-mardi-pause");
	    $template->mardi_repas		 = $post->get("$matricule-mardi-repas");
	    // mercredi
	    $template->mercredi_code	 = $post->get("$matricule-mercredi-code");
	    $template->mercredi_entree	 = $post->get("$matricule-mercredi-entree");
	    $template->mercredi_sortie	 = $post->get("$matricule-mercredi-sortie");
	    $template->mercredi_pause	 = $post->get("$matricule-mercredi-pause");
	    $template->mercredi_repas	 = $post->get("$matricule-mercredi-repas");
	    // jeudi
	    $template->jeudi_code		 = $post->get("$matricule-jeudi-code");
	    $template->jeudi_entree		 = $post->get("$matricule-jeudi-entree");
	    $template->jeudi_sortie		 = $post->get("$matricule-jeudi-sortie");
	    $template->jeudi_pause		 = $post->get("$matricule-jeudi-pause");
	    $template->jeudi_repas		 = $post->get("$matricule-jeudi-repas");
	    // vendredi
	    $template->vendredi_code	 = $post->get("$matricule-vendredi-code");
	    $template->vendredi_entree	 = $post->get("$matricule-vendredi-entree");
	    $template->vendredi_sortie	 = $post->get("$matricule-vendredi-sortie");
	    $template->vendredi_pause	 = $post->get("$matricule-vendredi-pause");
	    $template->vendredi_repas	 = $post->get("$matricule-vendredi-repas");
	    // samedi
	    $template->samedi_code		 = $post->get("$matricule-samedi-code");
	    $template->samedi_entree	 = $post->get("$matricule-samedi-entree");
	    $template->samedi_sortie	 = $post->get("$matricule-samedi-sortie");
	    $template->samedi_pause		 = $post->get("$matricule-samedi-pause");
	    $template->samedi_repas		 = $post->get("$matricule-samedi-repas");
	    // dimanche
	    $template->dimanche_code	 = $post->get("$matricule-dimanche-code");
	    $template->dimanche_entree	 = $post->get("$matricule-dimanche-entree");
	    $template->dimanche_sortie	 = $post->get("$matricule-dimanche-sortie");
	    $template->dimanche_pause	 = $post->get("$matricule-dimanche-pause");
	    $template->dimanche_repas	 = $post->get("$matricule-dimanche-repas");

	    // Sauvegarde
	    $employeTemplateTable->save($template);
	}

	return array(
	    'post' => $post,
	);
    }

    public function newCopy($site, $template) {
	// tables
	$employeTable		 = $this->getService('Table\EmployeTable');
	$employeTemplateTable	 = $this->getService('Table\EmployeTemplateTable');
	$siteTable		 = $this->getService('Table\SiteTable');
	$codeActiviteTable	 = $this->getService('Table\CodeActiviteTable');

	// Planning Type
	$templateList	 = $employeTemplateTable->getTemplates($template);
	$templates	 = array();
	// Parcours des templates pour isoler les employés qui ne sont plus dans dans le planning ou sont inactif
	foreach ($templateList as $tpl) {
	    $matricule = $tpl->matricule;
	    try {
		$employe = $employeTable->get($matricule);
	    }
	    catch (\Exception $ex) {
		$data	 = array(
		    'site_id'	 => -1,
		    'actif'		 => 0,
		);
		$employe = new \RHX\Model\Object\Employe($employeTable->getAdapter(), $data);
	    }
	    // Test
	    if ((int) $employe->site_id == $site && (int) $employe->actif != 0) {
		$templates[$matricule] = $tpl;
	    }
	}

	// Site
	$site = $siteTable->get($site);

	// COuleurs
	$colors		 = array();
	$codeList	 = $codeActiviteTable->getAll();
	foreach ($codeList as $c) {
	    $colors[$c->code] = $c->couleur;
	}

	return array(
	    'templates'	 => $templates,
	    'site'		 => $site,
	    'colors'	 => $colors,
	);
    }

    public function popinAction() {
	// Changement de Layout
	$this->layout('layout/ajax');

	// Table
	$TemplateTable	 = $this->getService('Table\EmployeTemplateTable');
	$codeTable	 = $this->getService('Table\CodeActiviteTable');

	// Post
	$post = $this->getRequest()
		->getPost();

	// Variables
	$id		 = $post->get('id');
	$matricule	 = $post->get('matricule');
	$prefix		 = $post->get('prefix');

	/**
	 * @var \RHX\Model\Object\EmployeTemplate $template
	 */
	$template = $TemplateTable->get($id);

	// Variables
	$code	 = 3;
	$entree	 = '00:00';
	$sortie	 = '00:00';
	$pause	 = 0;
	$repas	 = 0;

	// Assignation
	switch ($prefix) {
	    case 'lundi':
		$code	 = $template->lundi_code;
		$entree	 = $template->lundi_entree;
		$sortie	 = $template->lundi_sortie;
		$pause	 = $template->lundi_pause;
		$repas	 = $template->lundi_repas;
		break;
	    case 'mardi':
		$code	 = $template->mardi_code;
		$entree	 = $template->mardi_entree;
		$sortie	 = $template->mardi_sortie;
		$pause	 = $template->mardi_pause;
		$repas	 = $template->mardi_repas;
		break;
	    case 'mercredi':
		$code	 = $template->mercredi_code;
		$entree	 = $template->mercredi_entree;
		$sortie	 = $template->mercredi_sortie;
		$pause	 = $template->mercredi_pause;
		$repas	 = $template->mercredi_repas;
		break;
	    case 'jeudi':
		$code	 = $template->jeudi_code;
		$entree	 = $template->jeudi_entree;
		$sortie	 = $template->jeudi_sortie;
		$pause	 = $template->jeudi_pause;
		$repas	 = $template->jeudi_repas;
		break;
	    case 'vendredi':
		$code	 = $template->vendredi_code;
		$entree	 = $template->vendredi_entree;
		$sortie	 = $template->vendredi_sortie;
		$pause	 = $template->vendredi_pause;
		$repas	 = $template->vendredi_repas;
		break;
	    case 'samedi':
		$code	 = $template->samedi_code;
		$entree	 = $template->samedi_entree;
		$sortie	 = $template->samedi_sortie;
		$pause	 = $template->samedi_pause;
		$repas	 = $template->samedi_repas;
		break;
	    case 'dimanche':
		$code	 = $template->dimanche_code;
		$entree	 = $template->dimanche_entree;
		$sortie	 = $template->dimanche_sortie;
		$pause	 = $template->dimanche_pause;
		$repas	 = $template->dimanche_repas;
		break;
	}

	$codeList	 = $codeTable->getTemplateCodes();
	$codes		 = array();
	// Parcours des codes d'activités
	foreach ($codeList as $c) {
	    $codes[$c->id] = $c;
	}

	return array(
	    'template'	 => $template,
	    'code'		 => $code,
	    'entree'	 => $entree,
	    'sortie'	 => $sortie,
	    'codes'		 => $codes,
	    'prefix'	 => $prefix,
	    'id'		 => $id,
	    'matricule'	 => $matricule,
	    'pause'		 => $pause,
	    'repas'		 => $repas,
	);
    }

    public function saveAction() {
	//Changement de template
	$this->layout('layout/ajax');

	// Data POST
	$post = $this->getRequest()
		->getPost();

	// Variables
	$id	 = $this->params()
		->fromRoute('id', 0);
	$prefix	 = $post->get('prefix');
	$code	 = $post->get('code');
	$entree	 = $post->get('entree');
	$sortie	 = $post->get('sortie');
	$pause	 = $post->get('pause');
	$repas	 = $post->get('repas');

	// Tables
	$EmployeTemplateTable = $this->getService('Table\EmployeTemplateTable');

	// Planning Type de l'employé
	$planningType = $EmployeTemplateTable->get($id);

	// Assignation
	switch ($prefix) {
	    case 'lundi':
		$planningType->lundi_code	 = $code;
		$planningType->lundi_entree	 = $entree;
		$planningType->lundi_sortie	 = $sortie;
		$planningType->lundi_pause	 = $pause;
		$planningType->lundi_repas	 = $repas;
		break;

	    case 'mardi':
		$planningType->mardi_code	 = $code;
		$planningType->mardi_entree	 = $entree;
		$planningType->mardi_sortie	 = $sortie;
		$planningType->mardi_pause	 = $pause;
		$planningType->mardi_repas	 = $repas;
		break;

	    case 'mercredi':
		$planningType->mercredi_code	 = $code;
		$planningType->mercredi_entree	 = $entree;
		$planningType->mercredi_sortie	 = $sortie;
		$planningType->mercredi_pause	 = $pause;
		$planningType->mercredi_repas	 = $repas;
		break;

	    case 'jeudi':
		$planningType->jeudi_code	 = $code;
		$planningType->jeudi_entree	 = $entree;
		$planningType->jeudi_sortie	 = $sortie;
		$planningType->jeudi_pause	 = $pause;
		$planningType->jeudi_repas	 = $repas;
		break;

	    case 'vendredi':
		$planningType->vendredi_code	 = $code;
		$planningType->vendredi_entree	 = $entree;
		$planningType->vendredi_sortie	 = $sortie;
		$planningType->vendredi_pause	 = $pause;
		$planningType->vendredi_repas	 = $repas;
		break;

	    case 'samedi':
		$planningType->samedi_code	 = $code;
		$planningType->samedi_entree	 = $entree;
		$planningType->samedi_sortie	 = $sortie;
		$planningType->samedi_pause	 = $pause;
		$planningType->samedi_repas	 = $repas;
		break;

	    case 'dimanche':
		$planningType->dimanche_code	 = $code;
		$planningType->dimanche_entree	 = $entree;
		$planningType->dimanche_sortie	 = $sortie;
		$planningType->dimanche_pause	 = $pause;
		$planningType->dimanche_repas	 = $repas;
		break;
	}

	// Sauvegarde
	$EmployeTemplateTable->save($planningType);

	return array(
	    'post'		 => $post,
	    'planningType'	 => $planningType,
	);
    }

    public function calculateCumulAction() {
	// POST
	$post = $this->getRequest()
		->getPost();

	// Table
	$templateTable = new \RHX\Model\Table\EmployeTemplateTable($this->getAdapter());

	$template = $templateTable->get($post->id);

	// Calcul
	$cumul	 = new RHDateTime();
	$cumul->setTime();
	// Règles
	$rules	 = new XRules($this->getAdapter());
	$data	 = $rules->Calcul($template->lundi_code, $post->matricule, $template->lundi_entree, $template->lundi_sortie, $template->lundi_pause, $template->lundi_repas, 1.0);
	$cumul->addHours($data->tempsDiff, false);
	$data	 = $rules->Calcul($template->mardi_code, $post->matricule, $template->mardi_entree, $template->mardi_sortie, $template->mardi_pause, $template->mardi_repas, 1.0);
	$cumul->addHours($data->tempsDiff, false);
	$data	 = $rules->Calcul($template->mercredi_code, $post->matricule, $template->mercredi_entree, $template->mercredi_sortie, $template->mercredi_pause, $template->mercredi_repas, 1.0);
	$cumul->addHours($data->tempsDiff, false);
	$data	 = $rules->Calcul($template->jeudi_code, $post->matricule, $template->jeudi_entree, $template->jeudi_sortie, $template->jeudi_pause, $template->jeudi_repas, 1.0);
	$cumul->addHours($data->tempsDiff, false);
	$data	 = $rules->Calcul($template->vendredi_code, $post->matricule, $template->vendredi_entree, $template->vendredi_sortie, $template->vendredi_pause, $template->vendredi_repas, 1.0);
	$cumul->addHours($data->tempsDiff, false);
	$data	 = $rules->Calcul($template->samedi_code, $post->matricule, $template->samedi_entree, $template->samedi_sortie, $template->samedi_pause, $template->samedi_repas, 1.0);
	$cumul->addHours($data->tempsDiff, false);
	$data	 = $rules->Calcul($template->dimanche_code, $post->matricule, $template->dimanche_entree, $template->dimanche_sortie, $template->dimanche_pause, $template->dimanche_repas, 1.0);
	$cumul->addHours($data->tempsDiff, false);

	// Employé
	$employe	 = $template->getEmploye();
	$dataCP		 = $employe->getDataCP();
	$cumulFloat	 = RHDateTime::toFloat($cumul->totalHours());
	$state		 = EmployeTemplate::EQUAL;
	if ($cumulFloat < $dataCP['hebdo']) {
	    $state = EmployeTemplate::LESS_THAN;
	}
	elseif ($cumulFloat > $dataCP['hebdo']) {
	    $state = EmployeTemplate::GREATER_THAN;
	}
	elseif ($cumulFloat == $dataCP['hebdo']) {
	    $state = EmployeTemplate::EQUAL;
	}

	return new JsonModel(array(
	    'cumul'	 => $cumul->totalHours(),
	    'state'	 => $state,
	));
    }

    /**
     * Effacer un planning type
     * 
     * @return \Zend\View\Model\JsonModel
     */
    public function eraseAction() {
	// changement de layout
	$this->layout('layout/ajax');
	// post
	$post			 = $this->getRequest()
		->getPost();
	// tables
	$tablePlanningTemplate	 = new \RHX\Model\Table\PlanningTemplateTable($this->getAdapter());
	$tableEmployeTemplate	 = new \RHX\Model\Table\EmployeTemplateTable($this->getAdapter());
	// Template en cours
	$template		 = $tablePlanningTemplate->get($post->id);
	// suppression des plannings employes
	$manager = new \RHX\Model\Manager\TemplateManager($this->getAdapter());
	$nb = 0;
	$error = "";
	if(!$manager->isUsed($template->template)){
	    $nb			 = $tableEmployeTemplate->delete(array('template' => $template->template));
	    $n			 = $tablePlanningTemplate->delete(array('id' => $template->id));
	}else{
	    $error = "Un planning type ne peut pas être effacé si il a servi à créér un planning semaine";
	}

	return new JsonModel(array(
	    'done'		 => ($n > 0) ? true : false,
	    'template'	 => $template,
	    'nb'		 => $nb,
	    'error' => $error,
	));
    }

    /**
     * Mise à jour de tous les plannings types
     * 
     * @return array
     */
    public function updatePlanningTemplateAction(){
	$navigation = new \Zend\Session\Container('navigation');
	$site = $navigation->offsetGet('site');
	\Zend\Debug\Debug::dump($site, "Site");
	$manager = new \RHX\Model\Manager\TemplateManager($this->getAdapter());
	$manager->updateAllTemplate($site->id);
	$this->layout('layout/ajax');
	return array();
    }
}
